﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TCHSHue.DAL;

namespace TCHSHue.BLL
{
    public class TrangThaiHoSoBLL
    {
        internal static object getList()
        {
            using (QLHSHueDataContext data = new QLHSHueDataContext())
            {
                var ls = from e in data.TrangThaiHoSos
                         orderby e.maTrangThai ascending
                         select e;
                return ls.ToList();
            }
        }

        internal static TrangThaiHoSo getTrangThai(string maTrangThai)
        {
            int temp = 0;
            if (int.TryParse(maTrangThai, out temp))
            {
                using (QLHSHueDataContext data = new QLHSHueDataContext())
                {
                    var trangthai = (from e in data.TrangThaiHoSos
                                     where e.maTrangThai == int.Parse(maTrangThai)
                                     select e).SingleOrDefault();
                    return trangthai;
                }
            }
            return null;
        }

        internal static bool create(string maTrangThai, string tenTrangThai)
        {
            int temp = 0;
            if (int.TryParse(maTrangThai, out temp))
            {
                using (QLHSHueDataContext data = new QLHSHueDataContext())
                {
                    var ls = (from e in data.TrangThaiHoSos
                              where e.maTrangThai == int.Parse(maTrangThai)
                              select e).SingleOrDefault();
                    if (ls != null || string.IsNullOrEmpty(tenTrangThai))
                        return false;
                    TrangThaiHoSo tths = new TrangThaiHoSo { maTrangThai = int.Parse(maTrangThai), noiDungTrangThai = tenTrangThai };
                    data.TrangThaiHoSos.InsertOnSubmit(tths);
                    data.SubmitChanges();
                    return true;
                }
            }
            else
                return false;
        }

        internal static bool update(string maTrangThai, string tenTrangThai)
        {
            int temp = 0;
            if (int.TryParse(maTrangThai, out temp))
            {
                using (QLHSHueDataContext data = new QLHSHueDataContext())
                {
                    var ls = (from e in data.TrangThaiHoSos
                              where e.maTrangThai == int.Parse(maTrangThai)
                              select e).SingleOrDefault();
                    if (ls != null || string.IsNullOrEmpty(tenTrangThai))
                    {
                        ls.noiDungTrangThai = tenTrangThai;
                        data.SubmitChanges();
                        return true;
                    }
                    else
                        return false;
                }
            }
            else
                return false;
        }

        internal static bool delete(string maTrangThai)
        {
            int temp = 0;
            if (int.TryParse(maTrangThai, out temp))
            {
                using (QLHSHueDataContext data = new QLHSHueDataContext())
                {
                    var tths = (from e in data.TrangThaiHoSos
                                where e.maTrangThai == int.Parse(maTrangThai)
                                select e).SingleOrDefault();
                    if (tths != null)
                    {
                        data.TrangThaiHoSos.DeleteOnSubmit(tths);
                        data.SubmitChanges();
                        return true;
                    }
                    return false;
                }
            }
            else
                return false;
        }
    }
}